package net.top.viewer.utils
{
	
	import mx.core.FlexGlobals;
	
	import spark.utils.LabelUtil;
	
	public final class LabelUtil
	{
		public static function findLongestLabelItem(items:Array,
													labelField:String = null,
													labelFunction:Function = null):Object
		{
			var longestItem:Object;
			
			var currentItemWidth:Number;
			var longestItemLabelWidth:Number = 0;
			var currentItemLabel:String;
			
			for each (var item:Object in items)
			{
				currentItemLabel = itemToLabel(item, labelField, labelFunction);
				
				currentItemWidth =
					FlexGlobals.topLevelApplication.measureText(currentItemLabel).width;
				
				if (currentItemWidth > longestItemLabelWidth)
				{
					longestItemLabelWidth = currentItemWidth;
					longestItem = item;
				}
			}
			
			return longestItem;
		}
		
		private static function itemToLabel(item:Object,
											labelField:String = null,
											labelFunction:Function = null):String
		{
			return spark.utils.LabelUtil.itemToLabel(item, labelField, labelFunction);
		}
	}
}
